import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login.vue'
import myinfo from '../views/myinfo.vue'
import layout from '../views/layout.vue'
import find from '../views/find.vue'
import shuati from '../views/shuati.vue'
import company from '../views/company/company.vue'

import {getToken} from '@/utils/token.js'
import  { Toast } from 'vant'



Vue.use(VueRouter)


const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/',
    name: 'layout',
    component: layout,
    // 二级路由
    children:[
       {
         //公司
    path: '/company',
    name: 'company',
    component: company,
  },
  {
    //我的
    path: '/myinfo',
    name: 'myinfo',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: myinfo,

// 配合导航守卫
    meta:{isLogin:true}
  }
  ,
  {
    //发现
    path: '/',
    name: 'find',
    component: find
  }
  ,
  {
    //刷题
    path: '/shuati',
    name: 'shuati',
    component: shuati,
    meta:{isLogin:true}
  }
    ]
  },
 
]


const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.meta.isLogin&&!getToken()){
    Toast.fail('请先登录')
    next({path:'/login'})
  }else{
    next()
  }

})


export default router